package com.csw.shuanfa.utils.id;

import cn.hutool.core.util.IdUtil;

import java.math.BigInteger;
import java.util.UUID;

/**
 * 【生成8位随机字符串（重复概率1/218万亿）】https://mbd.baidu.com/ma/s/uIZ7gbyn
 * 【JAVA生成短8位不重复的UUID】https://mbd.baidu.com/ma/s/DUjU5agp
 * 这两个内容一样
 */
public class UUIDUtilTest {

    public static void main(String[] args) throws InterruptedException {
        //8
        System.out.println("uuid8】" + generateShortUuid8() + "】" + generateShortUuid8().length() + "位");

        //32
        System.out.println("simpleUUID】" + IdUtil.simpleUUID() + "】" + IdUtil.simpleUUID().length() + "位");//每次用一个random
        //22位
        System.out.println("UUIDtoBase62Id】" + UUIDtoBase62Id() + "】" + UUIDtoBase62Id().length() + "位");

        //24【推荐】
        System.out.println("objectId】" + IdUtil.objectId() + "】" + IdUtil.objectId().length() + "位");
        //16位【推荐】
        System.out.println("objectId62】" + ObjectIdIdtoBase62Id() + "】" + ObjectIdIdtoBase62Id().length() + "位");
        //24【推荐】
        System.out.println("objectId2】" + ObjectIdUtil.next() + "】" + ObjectIdUtil.next().length() + "位");
        //21
        System.out.println("nanoId】" + IdUtil.nanoId() + "】" + IdUtil.nanoId().length() + "位");
        //12
        System.out.println("nanoId12】" + IdUtil.nanoId(12) + "】" + IdUtil.nanoId(12).length() + "位");
        //19
        System.out.println("getSnowflake】" + IdUtil.getSnowflake().nextId() + "】" + String.valueOf(IdUtil.getSnowflake().nextId()).length() + "位");
        //12【推荐】可以全是大写字母或者转为小写字母
        System.out.println("getSnowflake_36】" + getSnowflake_36() + "】" + getSnowflake_36().length() + "位");
        //11【推荐】字母大小写固定不能转
        System.out.println("SnowflaketoBase62Id】" + SnowflakeIdtoBase62Id() + "】" + SnowflakeIdtoBase62Id().length() + "位");
        //11
        System.out.println("getNaoTimeIdTest】" + getNaoTimeIdTest() + "】" + getNaoTimeIdTest().length() + "位");
        //19
        SnowFlakeIdUtil sm = new SnowFlakeIdUtil(1, 1);
        String s = sm.nextId() + "";
        System.out.println("getNaoTimeIdTest】" + s + "】" + s.length() + "位");
    }

    /**
     * UUID转8位id
     */
    public static String generateShortUuid8() {
        int length = 8;
        String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
                "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z"};
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < length; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();

    }

    /**
     *
     */
    private static final char[] BASE_62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();

    /**
     * 雪花转62进制
     */
    public static String SnowflakeIdtoBase62Id() {
        Long snowflakeId = IdUtil.getSnowflake().nextId();
        return longtoBase62Id(snowflakeId);
    }

    public static String longtoBase62Id(Long snowflakeId) {
        StringBuilder base62 = new StringBuilder();
        do {
            int remainder = (int) (snowflakeId % 62);
            base62.insert(0, BASE_62_CHARS[remainder]);
            snowflakeId = snowflakeId / 62;
        } while (snowflakeId > 0);

        return base62.toString();
    }

    /**
     * objectId转62进制
     */
    public static String ObjectIdIdtoBase62Id() {
        BigInteger bigInteger = new BigInteger(IdUtil.objectId(), 16);
        return bigIntegerToBase62(bigInteger);
    }

    public static String bigIntegerToBase62(BigInteger decimal) {
        StringBuilder sb = new StringBuilder();
        while (decimal.compareTo(BigInteger.ZERO) > 0) {
            BigInteger[] divRem = decimal.divideAndRemainder(BigInteger.valueOf(62));
            sb.append(BASE_62_CHARS[divRem[1].intValue()]);
            decimal = divRem[0];
        }
        return sb.length() == 0 ? "0" : sb.reverse().toString();
    }

    /**
     * uuid16进制转62进制
     */
    public static String UUIDtoBase62Id() {
        BigInteger bigInteger = new BigInteger(IdUtil.simpleUUID(), 16);
        return bigIntegerToBase62(bigInteger);
    }
    /**
     * 毫秒实现
     */
    public static String getNaoTimeIdTest() throws InterruptedException {

        String a = System.currentTimeMillis() + "";
        String b = System.nanoTime() + "";
        //最后两位不要，一般也取不到
        b = b.substring(b.length() - 6, b.length() - 2);
        String res = a + b;
        return longtoBase62Id(Long.valueOf(res));
    }

    /**
     * 雪花转36进制
     *
     * @return
     */
    public static String getSnowflake_36() {
        return Long.toUnsignedString(IdUtil.getSnowflake().nextId(), 36).toUpperCase();
    }


}
